/* Gauge - Performance Measurement Framework for Mobile Platforms
 * Copyright (C) 2013, Juha Hytonen <juha.a.hytonen@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gauge.ExampleBenchmarks.Workloads;

import org.gauge.ExampleBenchmarks.Workloads.WBSCallback;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.webkit.WebView;
import android.widget.LinearLayout;

/**
 * Displays web pages. Controlled by WebBrowsing via WebBrowsingService
 */
public class WebBrowsingActivity extends Activity implements ServiceConnection {

	
	private final WBSCallback callback = new WBSCallback.Stub() {
		@Override
		public void GoTo(String url) {
			goTo(url);
		}

		@Override
		public void Exit() {
			exit();
		}
	};
	private WBSInterface service;
	private LinearLayout layout;
	private WebView webView;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT);
        
        webView = new WebView(this);
        LinearLayout.LayoutParams webViewParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT);
        layout.addView(webView);
        
        setContentView(layout);
    }

    @Override
    protected void onStart() {
    	super.onStart();
		Intent serviceIntent = new Intent(this, WebBrowsingService.class);
		bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
    }

	@Override
	public void onServiceConnected(ComponentName className, IBinder binder) {
		service = WBSInterface.Stub.asInterface(binder);
		try {
			service.SetCallback(callback);
		}
		catch(RemoteException e) {
		}
	}

	@Override
	public void onServiceDisconnected(ComponentName className) {
		service = null;
	}
	
	private void goTo(final String url) {
		runOnUiThread(new Runnable() {
			public void run() {
				webView.loadUrl(url);
			}
		});
	}

	private void exit() {
		unbindService(this);
		runOnUiThread(new Runnable() {
			public void run() {
				layout.removeView(webView);
				webView.removeAllViews();
				webView.destroy();
				finish();
			}
		});
	}
    
}
